package com.example.study.controller;


import com.example.study.common.api.ResultUtil;
import com.example.study.model.StudentVO;
import com.example.study.model.StudentPO;
import com.example.study.model.group.Insert;
import com.example.study.model.group.Update;
import com.example.study.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "学生管理")
@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 创建学生
     * */
    @ApiOperation("创建学生")
    @RequestMapping(value = "/create_student", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil createStudent(@RequestBody @Validated(Insert.class) StudentVO student){
        return studentService.saveStudent(student);
    }

    /**
     * 分页获取学生
     * */
    @ApiOperation("分页获取学生")
    @RequestMapping(value = "/student_page", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getStudentPage(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        return studentService.getStudentPage(pageIndex, pageSize);
    }

    /**
     * 根据条件分页获取学生
     * */
    @ApiOperation("根据条件分页获取学生")
    @RequestMapping(value = "/student_criteria_page", method = RequestMethod.GET)
    @ResponseBody
    public ResultUtil getStudentCriteriaPage(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @RequestBody StudentVO studentVO){
        return studentService.getStudentCriteriaPage(pageIndex, pageSize, studentVO);
    }

    /**
     * 更新学生信息
     * */
    @ApiOperation("更新学生信息")
    @RequestMapping(value = "/update_student", method = RequestMethod.PUT)
    @ResponseBody
    public ResultUtil updateStudent(@RequestBody @Validated(Update.class) StudentVO student){
        return studentService.updateStudent(student);
    }

    /**
     * 禁用学生
     * */
    @ApiOperation("禁用学生")
    @RequestMapping(value = "/delete_student/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultUtil deleteStudent(@PathVariable(value = "id") Long id){
        return studentService.deleteStudent(id);
    }
}
